﻿namespace RPCenter.WindowsAPI
{
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.IO;
    using System.Security.Principal;
    using System.Text;

    public class NativeOperatingSystem
    {
        public static void DeleteUserProfile(string UsernameToDelete)
        {
            NTAccount account = new NTAccount(UsernameToDelete);
            if (!ApiDefinitions.DeleteProfile(account.Translate(typeof(SecurityIdentifier)).Value, null, null))
            {
                throw new ApplicationException("The user profile could not be deleted - the last error reported was: " + new Win32Exception().Message);
            }
        }

        public static void InstallFontForCurrentSession(string FontFile)
        {
            if (!File.Exists(FontFile))
            {
                throw new FileNotFoundException("The specified font file could not be found (" + FontFile + ")", FontFile);
            }
            if (ApiDefinitions.AddFontResource(FontFile) == 0)
            {
                throw new ApplicationException("The font (" + FontFile + ")  was not successfully installed. No extended error information is available");
            }
            ApiDefinitions.SendMessage(ApiDefinitions.HWND_BROADCAST, 0x1d, 0, 0);
        }

        public static void RefreshDevices()
        {
            ApiDefinitions.SP_DEVINFO_DATA sp_devinfo_data = new ApiDefinitions.SP_DEVINFO_DATA();
            uint num = ApiDefinitions.CM_Locate_DevNode(ref sp_devinfo_data.DevInst, 0, 0L);
            if (num != 0L)
            {
                throw new ApplicationException("Unable to refresh devices, CM_Locate_DevNode returned the following error code: " + Conversions.ToString(num));
            }
            uint num2 = ApiDefinitions.CM_Reenumerate_DevNode(sp_devinfo_data.DevInst, 0L);
            if (num2 != 0L)
            {
                throw new ApplicationException("Unable to refresh devices, CM_Reenumerate_DevNode returned the following error code: " + Conversions.ToString(num2));
            }
        }

        public static void RemoveFontForCurrentSession(string FontFile)
        {
            if (!File.Exists(FontFile))
            {
                throw new FileNotFoundException("The specified font file could not be found (" + FontFile + ")", FontFile);
            }
            if (ApiDefinitions.RemoveFontResource(FontFile) == 0)
            {
                throw new ApplicationException("The font (" + FontFile + ")  was not successfully removed. No extended error information is available");
            }
            ApiDefinitions.SendMessage(ApiDefinitions.HWND_BROADCAST, 0x1d, 0, 0);
        }

        public static void RenameComputer(string NewName)
        {
            ApiDefinitions.NET_API_STATUS net_api_status = ApiDefinitions.NetRenameMachineInDomain(null, NewName, null, null, 2);
            if (net_api_status == ApiDefinitions.NET_API_STATUS.NERR_SETUPNOTJOINED)
            {
                if (!ApiDefinitions.SetComputerNameEx(ApiDefinitions.COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname, NewName))
                {
                    throw new ApplicationException("The computer could not be renamed - the last error reported was: " + new Win32Exception().Message);
                }
            }
            else if (net_api_status != ApiDefinitions.NET_API_STATUS.NERR_SUCCESS)
            {
                throw new ApplicationException("The computer could not be renamed - the error code reported was: " + net_api_status.ToString());
            }
        }

        public static bool Is64BitOperatingSystem
        {
            get
            {
                return ((IntPtr.Size == 8) || NativeProcess.IsWow64Process(Process.GetCurrentProcess()));
            }
        }

        public static string UserProfilesDirectory
        {
            get
            {
                uint num = 0;
                ApiDefinitions.GetProfilesDirectory(null, ref num);
                StringBuilder lpProfilesDir = new StringBuilder((int) num);
                ApiDefinitions.GetProfilesDirectory(lpProfilesDir, ref num);
                return lpProfilesDir.ToString();
            }
        }

        public enum ShutdownOption : uint
        {
            LogOff = 0,
            Restart = 2,
            Shutdown = 8
        }
    }
}

